﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Royaume
{

    public partial class Construction : System.Web.UI.Page
    {
        SqlConnection cn;
        SqlConnection cn2;
        SqlCommand cmd;
        SqlCommand cmdCTempo;
        TableCell td;
        TableRow tr;
        cSql objSql = new cSql("(local)", "DB_GAME", "RoyaumesLibres", "123456");
        Dictionary<SqlParameter, object> param = new Dictionary<SqlParameter, object>();

        Button btnAnnuler = new Button();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.cn = Connect.getconnectionstringG();
            this.cn2 = Connect.getconnectionstringG();
            this.fnAfficherConstructionDispo();
            this.fnAfficherCCompletee();
        }

        protected void fnAfficherConstructionDispo()
        {
            this.cn.Open();
            this.cn2.Open();
            this.T_ConstructionDispo.Controls.Clear();

            SqlDataReader dr;
            this.cmd = new SqlCommand();
            this.cmd.Connection = this.cn;

          //  this.cmd.CommandText = "spGetInfoBatiment";
            this.cmd.CommandText = "spShowBuilding";
            this.cmd.CommandType = CommandType.StoredProcedure;
            param.Clear();
            cmd.Parameters.AddWithValue("@pkJoueur", Session["pkUser"]); 

            dr = cmd.ExecuteReader();

            this.cmdCTempo = new SqlCommand();
            this.cmdCTempo.Connection = this.cn2;

            this.cmdCTempo.CommandText = "spGetInfoTempo";
            this.cmdCTempo.CommandType = CommandType.StoredProcedure;
            param.Clear(); 
            cmdCTempo.Parameters.AddWithValue("@FkMembre", Session["pkUser"]); 

            SqlDataReader result = cmdCTempo.ExecuteReader();
            result.Read();

            if (dr != null)
            {
                tr = new TableRow();
                //entete 
                td = new TableCell();

                td.Text = "Nom";
                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = "Description";
                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = "Quantite de bois";
                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = "Quantite de minerai";
                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = "Temps Requis (heures)";
                tr.Cells.Add(td);
                td = new TableCell();

                this.T_ConstructionDispo.Controls.Add(tr);

                while (dr.Read())
                {
                    tr = new TableRow();

                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                      td = new TableCell();
                     
                      this.T_ConstructionDispo.Controls.Add(tr);
                        if (i == dr.FieldCount-1)
                        {
                            Button btn = new Button();
                           
                            //si oui ca change pour le bouton annuler
                            if (result != null && result.HasRows)
                            {
                               
                               if (result["FkBatiment"] == dr["Pk"])
                               {

                                    //btnAnnuler
                                    btn.Attributes.Add("FkBatiment", result["FkBatiment"].ToString());
                                    btn.Text = "Annuler";
                                    btn.ID = "btnAnnuler";
                                    btn.Click += new System.EventHandler(fnAnnulerConstruction);

                                    if (result["TRestant"] != result["TConstruction"])
                                    {
                                    btn.Enabled = false;
                                    }
                                }
                                else
                                {
                                        //BtnConstruire Desactiver
                                       btn.Attributes.Add("Pk", dr[5].ToString());
                                        btn.Text = "Construire";
                                        btn.ID = "btnContruire" + dr[0].ToString();
                                        btn.Click += new System.EventHandler(fnContruireBatiment);
                                        btn.Enabled = false;
                                }
                            }
                            else
                            {
                                //sinon btnconstruire actif
                                btn.Attributes.Add("Pk", dr[5].ToString());
                                btn.ID = "btnContruire"+dr[0].ToString();
                                btn.Text = "Construire";
                                btn.Click += new System.EventHandler(fnContruireBatiment);
                            }

                            td.Controls.Add(btn);
                        }
                        else
                        {
                            td.Text = dr[i].ToString();
                        }

                        tr.Controls.Add(td);
                    }
                    //Affiche les données
                    this.T_ConstructionDispo.Controls.Add(tr);
                }

                dr.Close();
                this.cn.Close();
                result.Close();
                this.cn2.Close();
            }
        }
        
        protected void fnAfficherCCompletee()
        {
            cn.Open();
            SqlDataReader drUser;
            this.cmd = new SqlCommand();
            this.cmd.Connection = this.cn;

            this.cmd.CommandText = "spGetConstructionDuJoueur";
            this.cmd.CommandType = CommandType.StoredProcedure;

            this.cmd.Parameters.Clear();
            cmd.Parameters.Add("@PkMembre", SqlDbType.Int).Value = Session["pkUser"];
            drUser = cmd.ExecuteReader();
            if (drUser != null)
            {
                while (drUser.Read())
                {
                    tr = new TableRow();

                    for (int i = 0; i < drUser.FieldCount; i++)
                    {
                        td = new TableCell();
                        td.Text = drUser[i].ToString();
                        tr.Controls.Add(td);
                    }
                    //Affiche les données
                    this.Table_ConstructionCompletee.Controls.Add(tr);
                }
                if (this.cn != null)
                {
                    this.cn.Close();
                }
            }
        }
        protected void fnContruireBatiment(object sender, EventArgs e)
        {
            //appel spAcheterBatiment
            Button btn;
            btn = (Button)sender;
        
            param.Clear();
            param.Add(new SqlParameter("@FkMembre", SqlDbType.Int), Session["pkUser"]);
            param.Add(new SqlParameter("@PkBatiment", SqlDbType.Int), btn.Attributes["Pk".ToString()]);
                      
           // Session["pk"]=btn.Attributes["Pk".ToString()];
          
            objSql.fnStoredProc("spAcheterBatiment", param);

            this.fnAfficherConstructionDispo();
             
        }

        protected void fnAnnulerConstruction(object sender, EventArgs e)
        {
            //A COMPLETER
           //appel spAnnulerAchatBatiment

           Button btn;
           btn = (Button)sender;
          
           param.Clear();
           param.Add(new SqlParameter("@FkMembre", SqlDbType.Int), Session["pkUser"]);

          objSql.fnStoredProc("spAnnulerAchatBatiment", param);
        }

    }

}



